package myapps.ttclient.baseclass_and_interface;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ProtocolException;

import myapps.ttclient.GlobalVariables;
import myapps.ttclient.protocol.message.AdminAuthentificationResponseMessage;
import myapps.ttclient.protocol.message.DoneMessage;
import myapps.ttclient.protocol.message.FileDetailsResponseMessage;
import myapps.ttclient.protocol.message.FileExploreResponseMessage;
import myapps.ttclient.protocol.message.KeyMemberAuthentificationResponseMessage;
import myapps.ttclient.protocol.message.NMSAuthentificationResponseMessage;
import myapps.ttclient.protocol.message.NoticeMessage;
import myapps.ttclient.protocol.message.RSAPublicKeyServerMessage;
import myapps.ttclient.protocol.message.ScreenSizeResponseMessage;
import myapps.ttclient.protocol.message.SignOutResponseMessage;
import myapps.ttclient.protocol.message.drawingscreen.DSOpenFileResponseMessage;
import myapps.ttclient.protocol.message.powerpoint.PPTOpenFileResponseMessage;
import myapps.ttclient.protocol.message.powerpoint.PPTTCPSlideShowScreenCaptureMessage;
import myapps.ttclient.protocol.message.powerpoint.PPTSlideShowDataResponseMessage;
import myapps.ttclient.protocol.message.powerpoint.PPTWindowsRelationshipResponseMessage;


public abstract class TTClientMessage {

	public static final int MOUSE_MOVE = 0;
	public static final int MOUSE_CLICK = 1;

	public static final int ADMIN_AUTHENTIFICATION_REQUEST = 4;
	public static final int ADMIN_AUTHENTIFICATION_RESPONSE = 5;

	public static final int FILE_EXPLORE_REQUEST = 8;
	public static final int FILE_EXPLORE_RESPONSE = 9;

	public static final int NOTICE = 110;

	public static final int NMS_AUTHENTIFICATION_REQUEST = 114;
	public static final int NMS_AUTHENTIFICATION_RESPONSE = 115;

	public static final int NMS_AUTHENTIFICATION_RESET_REQUEST = 119;

	public static final int SIGNOUT_REQUEST = 117;
	public static final int SIGNOUT_RESPONSE = 118;

	public static final int RSA_PUBLIC_KEY_CLIENT = 121;
	public static final int RSA_PUBLIC_KEY_SERVER = 122;

	public static final int PPT_TCP_SLIDESHOW_SCREEN_CAPTURE = 107;
	
	public static final int PPT_START_SLIDESHOW = 108;
	public static final int PPT_END_SLIDESHOW = 103;

	public static final int PPT_NEXT = 101;
	public static final int PPT_PREV = 102;

	public static final int PPT_SLIDESHOW_DATA_REQUEST = 111;
	public static final int PPT_SLIDESHOW_DATA_RESPONSE= 112;

	public static final int PPT_GO_TO_SLIDE = 113;

	public static final int PPT_OPEN_FILE_REQUEST = 123;
	public static final int PPT_OPEN_FILE_RESPONSE = 124;

	public static final int PPT_MAXIMIZE_WINDOW = 125;
	public static final int PPT_MINUMIZE_WINDOW = 126;

	public static final int PPT_LAUNCH_PPT_CONTROLLER = 127;
	public static final int PPT_QUIT_PPT_CONTROLLER = 128;

	public static final int PPT_CLOSE_FILE = 129;
	public static final int PPT_CLOSE_ALL_FILES = 130;

	public static final int PPT_WINDOWS_RELATIONSHIP_REQUEST = 133;
	public static final int PPT_WINDOWS_RELATIONSHIP_RESPONSE = 134;

	public static final int PPT_ACTIVATE_WINDOW = 135;

	public static final int PPT_ENABLE_BROADCASTING = 136;
	public static final int PPT_DISABLE_BROADCASTING = 137;

	public static final int DONE = 138;

	public static final int PPT_SLIDE_SHOW_CLICK = 139;

	public static final int PPT_BLACK_SCREEN = 140;
	public static final int PPT_WHITE_SCREEN = 141;
	
	public static final int DS_OPEN_FILE_REQUEST = 142;
    public static final int DS_OPEN_FILE_RESPONSE = 143;
    public static final int DS_LOAD_REQUEST = 144;
    public static final int DS_SAVE_RESPONSE = 145;

	public static final int DS_SHOW_DRAWING_SCREEN = 131;
	public static final int DS_HIDE_DRAWING_SCREEN = 132;
	
	public static final int KEY_MEMBER_AUTHENTIFICATION_REQUEST = 146;
	public static final int KEY_MEMBER_AUTHENTIFICATION_RESPONSE = 147;
	
	public static final int SCREEN_SIZE_REQUEST = 148;
	public static final int SCREEN_SIZE_RESPONSE = 149;
	
	public static final int FILE_DETAILS_REQUEST = 150;
    public static final int FILE_DETAILS_RESPONSE = 151;

	public static final int DS_ACTION = (byte) 201;
	public static final int DS_UNDO = (byte) 202;
	public static final int DS_REDO = (byte) 203;
	public static final int DS_CHANGECOLOR = (byte) 204;
	public static final int DS_LINE = (byte) 205;
	public static final int DS_FREEDOM = (byte) 206;
	public static final int DS_CIRCLE = (byte) 207;
	public static final int DS_ELLIPSE = (byte) 208;
	public static final int DS_RECTANGLE = (byte) 209;
	public static final int DS_POINTER = (byte) 210;
	public static final int DS_BEZIER = (byte) 211;
	public static final int DS_CLEAR =(byte)212;
	public static final int DS_CHANGEWIDTH = (byte)213;
	public static final int DS_CLIPART = (byte) 214;
	public static final int DS_CLEAR_CURRENT_SLIDE =(byte)215;
	public static final int DS_CLEAR_ALL_SLIDES =(byte)216;


	public static TTClientMessage parse(DataInputStream dis) throws IOException {
		byte type = dis.readByte();

		int length = dis.readInt();

		switch (type) {
		case RSA_PUBLIC_KEY_SERVER:
			return RSAPublicKeyServerMessage.parse(dis);

		case ADMIN_AUTHENTIFICATION_RESPONSE:
			return AdminAuthentificationResponseMessage.parse(dis);
			
		case NMS_AUTHENTIFICATION_RESPONSE:
			return NMSAuthentificationResponseMessage.parse(dis);
			
		case (byte) KEY_MEMBER_AUTHENTIFICATION_RESPONSE:
			return KeyMemberAuthentificationResponseMessage.parse(dis);
			
		case SIGNOUT_RESPONSE:
			return SignOutResponseMessage.parse(dis);
			
		case FILE_EXPLORE_RESPONSE:
			return FileExploreResponseMessage.parse(dis);
			
		case PPT_OPEN_FILE_RESPONSE:
			return PPTOpenFileResponseMessage.parse(dis);
			
		case (byte) DS_OPEN_FILE_RESPONSE:
			return DSOpenFileResponseMessage.parse(dis);
			
		case (byte) PPT_WINDOWS_RELATIONSHIP_RESPONSE:
			return PPTWindowsRelationshipResponseMessage.parse(dis);
		
		case PPT_TCP_SLIDESHOW_SCREEN_CAPTURE:
			return  PPTTCPSlideShowScreenCaptureMessage.parse(dis);
			
		case PPT_SLIDESHOW_DATA_RESPONSE:
			return  PPTSlideShowDataResponseMessage.parse(dis);
			
		case NOTICE:
			return NoticeMessage.parse(dis);
		
		case (byte) SCREEN_SIZE_RESPONSE:
			return ScreenSizeResponseMessage.parse(dis);
		
		case (byte) FILE_DETAILS_RESPONSE:
			return FileDetailsResponseMessage.parse(dis);
			
		case (byte) DONE:
			return DoneMessage.parse(dis);
		
		default:
			////////Message ko dc bit, ta se dua vao length de skip cai byte tiep theo
			if(length > 0) {
				byte[] tempBuffer = new byte[length];
				dis.read(tempBuffer);
			
			} else if (length == GlobalVariables.NO_LENGTH_CODE) {
				throw new ProtocolException();
			
			} else {
				throw new ProtocolException();
			
			}
			return null;
		}
	}

	public abstract void toDataOutputStream(DataOutputStream dos) throws IOException;
}
